using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VWBEDI
    {
        public DataRow Convert2WBEDI(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryWBEDI(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["WBEMANDANT"] = " ".PadRight(2 ); 
         dwReturn["WBELFDNR"] = " ".PadRight(0 ); 
         dwReturn["WBELFDPOS"] = " ".PadRight(0 ); 
         dwReturn["WBEMTYP"] = " ".PadRight(3 ); 
         dwReturn["WBEKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["WBELIEFNR"] = " ".PadRight(8 ); 
         dwReturn["WBELIEFSCH"] = " ".PadRight(10 ); 
         dwReturn["WBELIEFPOS"] = " ".PadRight(4 ); 
         dwReturn["WBEEINDAT"] = " ".PadRight(8 ); 
         dwReturn["WBEEINZEIT"] = " ".PadRight(4 ); 
         dwReturn["WBEEINPROG"] = " ".PadRight(8 ); 
         dwReturn["WBEBSTATUS"] = " ".PadRight(2 ); 
         dwReturn["WBEBMNR"] = " ".PadRight(4 ); 
         dwReturn["WBEBMPAR1"] = " ".PadRight(15 ); 
         dwReturn["WBEBMPAR2"] = " ".PadRight(10 ); 
         dwReturn["WBEBDATUM"] = " ".PadRight(8 ); 
         dwReturn["WBEBZEIT"] = " ".PadRight(4 ); 
         dwReturn["WBEDATEN1"] = " ".PadRight(254 ); 
         dwReturn["WBEDATEN2"] = " ".PadRight(254 ); 
         dwReturn["WBEDATEN3"] = " ".PadRight(254 ); 
         dwReturn["WBEDATEN4"] = " ".PadRight(134 ); 
         dwReturn["WBEFLAGS"] = " ".PadRight(150 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapWBEDI(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWBEDI(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWBEDI(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWBEDI(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertWBEDI(dwLocal, ITXDBVERSION);
            }
            UpdateWBEDI(dwLocal);
        }
        public void InsertWBEDI(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO WBEDI(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryWBEDI(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WBEDI WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM WBEDI  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "WBEDI";
            Return = Result;
            return Return;
        }
        public void DeleteWBEDI(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM WBEDI   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateWBEDI(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE WBEDI SET "
                    +"WBEMANDANT= :WBEMANDANT,"
                    +"WBELFDNR= :WBELFDNR,"
                    +"WBELFDPOS= :WBELFDPOS,"
                    +"WBEMTYP= :WBEMTYP,"
                    +"WBEKUNDNR= :WBEKUNDNR,"
                    +"WBELIEFNR= :WBELIEFNR,"
                    +"WBELIEFSCH= :WBELIEFSCH,"
                    +"WBELIEFPOS= :WBELIEFPOS,"
                    +"WBEEINDAT= :WBEEINDAT,"
                    +"WBEEINZEIT= :WBEEINZEIT,"
                    +"WBEEINPROG= :WBEEINPROG,"
                    +"WBEBSTATUS= :WBEBSTATUS,"
                    +"WBEBMNR= :WBEBMNR,"
                    +"WBEBMPAR1= :WBEBMPAR1,"
                    +"WBEBMPAR2= :WBEBMPAR2,"
                    +"WBEBDATUM= :WBEBDATUM,"
                    +"WBEBZEIT= :WBEBZEIT,"
                    +"WBEDATEN1= :WBEDATEN1,"
                    +"WBEDATEN2= :WBEDATEN2,"
                    +"WBEDATEN3= :WBEDATEN3,"
                    +"WBEDATEN4= :WBEDATEN4,"
                    +"WBEFLAGS= :WBEFLAGS,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("WBEMANDANT", OracleType.NVarChar).Value = dwData["WBEMANDANT"].ToString(); 
          or1.Parameters.Add("WBELFDNR", OracleType.NVarChar).Value = dwData["WBELFDNR"].ToString(); 
          or1.Parameters.Add("WBELFDPOS", OracleType.NVarChar).Value = dwData["WBELFDPOS"].ToString(); 
          or1.Parameters.Add("WBEMTYP", OracleType.NVarChar).Value = dwData["WBEMTYP"].ToString(); 
          or1.Parameters.Add("WBEKUNDNR", OracleType.NVarChar).Value = dwData["WBEKUNDNR"].ToString(); 
          or1.Parameters.Add("WBELIEFNR", OracleType.NVarChar).Value = dwData["WBELIEFNR"].ToString(); 
          or1.Parameters.Add("WBELIEFSCH", OracleType.NVarChar).Value = dwData["WBELIEFSCH"].ToString(); 
          or1.Parameters.Add("WBELIEFPOS", OracleType.NVarChar).Value = dwData["WBELIEFPOS"].ToString(); 
          or1.Parameters.Add("WBEEINDAT", OracleType.NVarChar).Value = dwData["WBEEINDAT"].ToString(); 
          or1.Parameters.Add("WBEEINZEIT", OracleType.NVarChar).Value = dwData["WBEEINZEIT"].ToString(); 
          or1.Parameters.Add("WBEEINPROG", OracleType.NVarChar).Value = dwData["WBEEINPROG"].ToString(); 
          or1.Parameters.Add("WBEBSTATUS", OracleType.NVarChar).Value = dwData["WBEBSTATUS"].ToString(); 
          or1.Parameters.Add("WBEBMNR", OracleType.NVarChar).Value = dwData["WBEBMNR"].ToString(); 
          or1.Parameters.Add("WBEBMPAR1", OracleType.NVarChar).Value = dwData["WBEBMPAR1"].ToString(); 
          or1.Parameters.Add("WBEBMPAR2", OracleType.NVarChar).Value = dwData["WBEBMPAR2"].ToString(); 
          or1.Parameters.Add("WBEBDATUM", OracleType.NVarChar).Value = dwData["WBEBDATUM"].ToString(); 
          or1.Parameters.Add("WBEBZEIT", OracleType.NVarChar).Value = dwData["WBEBZEIT"].ToString(); 
          or1.Parameters.Add("WBEDATEN1", OracleType.NVarChar).Value = dwData["WBEDATEN1"].ToString(); 
          or1.Parameters.Add("WBEDATEN2", OracleType.NVarChar).Value = dwData["WBEDATEN2"].ToString(); 
          or1.Parameters.Add("WBEDATEN3", OracleType.NVarChar).Value = dwData["WBEDATEN3"].ToString(); 
          or1.Parameters.Add("WBEDATEN4", OracleType.NVarChar).Value = dwData["WBEDATEN4"].ToString(); 
          or1.Parameters.Add("WBEFLAGS", OracleType.NVarChar).Value = dwData["WBEFLAGS"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
